<?php
/**
 * Created by PhpStorm.
 * Adminuser: ERHAO
 * Date: 2018/7/31
 * Time: 17:02
 */
namespace app\admin\controller;
use think\Request;
use think\Db;
class Adminuser extends Base
{
    //    管理用户
    public function index(){
        $query = "select * ,(case status when 0 then '已停用' else '已启用' end ) as stats from adminuser";
        $res=Db::query($query);
        $this->assign('res',$res);
        return $this->fetch();
    }
    public function add_user(){
        return $this->fetch();
    }
    public function user()
    {
        $id=input('post.id');
        $res=Db::table('adminuser')->where('id',$id)->find();
        if ($res['status'] ==0){
            $r = Db::table('adminuser')->where('id',$id)->update(['status' =>1]);
            if ($r){
                return 1;
            }
        }else{
            $r=Db::table('adminuser')->where('id',$id)->update(['status' =>0]);
            if ($r){
                return 1;
            }
        }
    }
    public function save(){
        if (\request()->isPost()){
            $data=input('post.');
            $pass=['password'=>md5(input('post.password'))];
            $data=array_merge($data,$pass);
            $res=Db::table('adminuser')->insert($data);
            if ($res){
                $this->success('添加成功！');
            }
        }
    }
    public function del_user(Request $request){
        if ($request->isAjax()){
            $id=$request->param('id');
            if ($id){
                $res=Db::table('adminuser')->where('id',$id)->delete();
                if ($res){
                    return 1;
                }
            }
        }
    }
}